package com.pactera.base.commons.vo;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 返回的json数据对象
 * @author haosheng.wenhs
 *
 */
@ApiModel(value = "RespJson", description = "返回的json数据对象")
public class RestResponse<T> {
    
    @ApiModelProperty(value = "返回码，200：成功；500：失败；401：未登录", example = "200")
    private int code;
    
    @ApiModelProperty(value = "提示信息", example = "成功")
    private String msg;
    
    @ApiModelProperty(value = "业务返回数据")
    private T data;
    
    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }


    /**
     * 请求失败返回json封装
     *
     * @param msg 提示信息
     * @return  RespJson
     */
    public static  <E> RestResponse<E> getFailRtnMap(String msg) {
        RestResponse<E> restResponse = new RestResponse<>();
        restResponse.setCode(500);
        restResponse.setMsg(msg);
        return restResponse;
    }

    /**
     * 请求成功返回json封装（非分页请求）
     *
     * @param msg      提示信息
     * @param data 业务数据对象
     * @return RespJson
     */
    public static  <E> RestResponse<E> getSuccessRtnMap(String msg, E data) {
        RestResponse<E> restResponse = new RestResponse<>();
        restResponse.setCode(0);
        restResponse.setMsg(msg);
        restResponse.setData(data);
        return restResponse;
    }

    /**
     * 请求成功返回json封装（分页请求）
     *
     * @param msg      提示信息
     * @param pageData 分页业务数据对象
     * @return RespJson
     */
    public static <E> RestResponse<PageData<E>> getSuccessPageRtnMap(String msg, PageData<E> pageData) {
        RestResponse<PageData<E>> restResponse = new RestResponse<>();
        restResponse.setCode(200);
        restResponse.setMsg(msg);
        if (pageData != null) {
            restResponse.setData(pageData);
        } else {
            restResponse.setData(new PageData<>());
        }
        return restResponse;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
